<!--
 * @Author: your name
 * @Date: 2020-11-17 11:12:52
 * @LastEditTime: 2020-11-25 11:40:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vue-next/Users/duodian/Documents/qugame/work-admin/src/views/equip/list.vue
-->
<template>
  <div>
    <div class="query">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item>
          <el-input
            clearable
            v-model="query.keyWord"
            placeholder="请输入关键词"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-cascader
            v-model="query.type"
            :options="selector"
            :props="defaultProps"
            @change="change"
            :show-all-levels="false"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-button type="primary" @click="showModal" class="add"
          >新增道具</el-button
        >
      </el-form>
    </div>
    <el-table :data="tableData" stripe style="width: 100%" @sort-change="sort">
      <el-table-column prop="name" label="道具名称" width="180">
      </el-table-column>
      <el-table-column prop="typeDesc" label="所属分类" width="180">
      </el-table-column>
      <el-table-column prop="peaceSkinWorthVoList" label="道具估值" width="180" sortable="custom">
        <template slot-scope="scope">
          <div
            v-for="price in scope.row.peaceSkinWorthVoList"
            :key="price.level"
          >
            <el-tag size="mini">{{ price.level }}</el-tag>
            级道具估值{{ price.worth }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="道具图片">
        <template slot-scope="scope">
          <div style="width:100px">
          <span v-if="!scope.row.pic">暂无</span>
          <el-image :src="scope.row.pic" v-else >
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
          </div>

        </template>
      </el-table-column>
      <el-table-column prop="address" label="拼图素材">
        <template slot-scope="scope">
          <div style="width:100px">
          <span v-if="!scope.row.standardPicUrl">暂无</span>
          <el-image :src="scope.row.standardPicUrl" v-else >
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
          </div>

        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showModal(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="tools">
      <el-pagination
        @size-change="sizeChange"
        @current-change="pageChange"
        :current-page="query.pageNum"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="listTotal"
      >
      </el-pagination>
    </div>
    <Modal
      :visible="visible"
      :ruleForm="ruleForm"
      :closeHandel="closeHandel"
      :levelChange="levelChange"
      :submitHandel="submitHandel"
      :change="changeOnAdd"
      :defaultProps="defaultProps"
      :selector="[...selector]"
    />
  </div>
</template>
<script>
import Modal from './modal'
const initForm = {
  rootType: '',
  type: '',
  pic: '',
  standardPicUrl: '',
  name: '',
  peaceSkinWorthVoList: [
    {
      level: 1,
      worth: ''
    }
  ],
  shortName: '',
  level: 1,
  levels: [1, 2, 3, 4, 5, 6, 7]
}
export default {
  components: {
    Modal
  },
  data () {
    return {
      selector: [],
      visible: false,
      ruleForm: Object.assign({}, initForm),
      tableData: [],
      listTotal: null,
      query: {
        keyWord: null,
        type: null,
        pageNum: 1,
        pageSize: 30
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'nodePath',
        checkStrictly: true
      }
    }
  },
  mounted () {
    this.fetchList()
    this.fetchSelector()
    console.log('test deploy 3345!!')
  },
  methods: {
    async fetchList () {
      this.params = this.query
      const result = await this.$http.get('/peace/selectPeaceSkin', {
        params: this.query
      })
      if (result.code !== '0') {
        this.$message.error(result.desc)
        return
      }
      this.tableData = result.data
      this.listTotal = result.count
    },
    async fetchSelector () {
      const result = await this.$http.post('peace/selector', {
        type: 0
      })
      if (result.code !== '0') {
        this.$message.error(result.desc)
        return
      }
      this.selector = result.data.props
    },
    async updatePeaceSkinInfo () {
      const result = await this.$http.post('/peace/updatePeaceSkinInfo', this.ruleForm)
      if (result.code !== '0') {
        this.$message.error(result.desc)
      }
    },
    // 查询清空排序相关字段
    search () {
      this.query.name = null
      this.query.sort = null
      this.query.pageNum = 1
      this.fetchList()
    },
    // 排序
    sort (item) {
      this.query.name = item.column.property
      this.query.sort = item.order === 'ascending' ? 'asc' : 'desc'
      this.fetchList()
    },
    pageChange (page) {
      this.query.pageNum = page
      this.fetchList()
      console.log(page)
    },
    sizeChange (size) {
      this.query.pageSize = size
      this.fetchList()
    },
    onSubmit () {
      console.log('submit!')
    },
    showModal (item) {
      this.visible = true
      if (!item.name) {
        this.ruleForm = Object.assign({}, initForm)
        this.ruleForm.modalType = 'add'
        this.ruleForm.title = '新增道具'
        return
      }
      this.ruleForm.modalType = 'edit'
      this.ruleForm.title = '编辑道具'
      this.ruleForm = Object.assign({}, this.ruleForm, item)
      this.ruleForm.level = item.peaceSkinWorthVoList.length
      this.ruleForm.peaceSkinWorthVoList = item.peaceSkinWorthVoList
      this.formatType(this.ruleForm.typeList)
      console.log(this.ruleForm)
    },
    closeHandel () {
      this.visible = false
    },
    // 改变等级
    levelChange (level) {
      this.ruleForm.peaceSkinWorthVoList = []
      for (let i = 0; i < level; i++) {
        this.ruleForm.peaceSkinWorthVoList.push({
          level: i + 1,
          worth: ''
        })
      }
    },
    // 新增/编辑确定按钮事件
    async submitHandel () {
      if (this.ruleForm.type instanceof Array) {
        this.ruleForm.type = this.ruleForm.type.pop()
      }
      if (this.ruleForm.modalType === 'add') {
        const result = await this.$http.post('/peace/addPeaceSkin', this.ruleForm)
        if (result.code !== '0') {
          this.$message.error(result.desc)
        }
        this.fetchList()
        this.closeHandel()
        return
      }
      await this.updatePeaceSkinInfo()

      this.fetchList()
      this.closeHandel()
    },
    change (data) {
      this.query.type = data[data.length - 1]
    },
    changeOnAdd (data) {
      this.ruleForm.type = data[data.length - 1]
    },
    formatType (type) {
      let selectorVal = []
      type.reduce((prev, cur, index) => {
        if (prev) {
          selectorVal.push(prev + '-' + cur)
          return prev + '-' + cur
        } else {
          selectorVal.push(cur + '')
          return cur
        }
      }, '')
      this.ruleForm.type = selectorVal
    }
  }
}
</script>
<style lang="scss" scoped>
.query {
  .add {
    float: right;
  }
}
.tools {
  margin-top: 15px;
  padding-right: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
